<?php

class Administrator_LoginController extends Zend_Controller_Action
{

    public function init()
    {
        $option=array(
    			"layout" => "login",
    			"layoutPath" => TEMPLATES_PATH."/administrator/"
    	);
    	$layout = Zend_Layout::startMvc($option);
    	$this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
    	$this->view->headLink()->appendStylesheet(TEMPLATES_URL.'/administrator/css/login.css');
    	$this->view->headScript()->appendFile(TEMPLATES_URL.'/administrator/js/jquery-1.7.2.min.js');
    	$this->view->headScript()->appendFile(PUBLIC_URL.'/js/config.js');
    	$this->view->headScript()->appendFile(TEMPLATES_URL.'/administrator/js/jquery.login.js');
    	$this->view->headTitle('Login - '._page_title_administrator);
    }

    public function indexAction()
    {
    	$auth = Zend_Auth_Admin::getInstance();
    	if ($auth->hasIdentity()) {			   		
	    	$this->_redirect('administrator/system'); 
    	}
    	if($this->_request->isPost()){
    		
    		$user = strip_tags($this->_request->getParam('username'));
    		$psw = strip_tags($this->_request->getParam('password'));
    		if(!empty($user) && !empty($psw)){
    			$mUser = new Administrator_Model_Users();
    			$mUser->username = $user;
    			$mUser->password = $psw;
    			$flag = false;
    			if($mUser->login()){
    				$flag = true;
    			}
    			if($flag == true){
    				$this->_redirect('administrator/system');
    			}
    		}
    	}
    }
    
    public function inAction(){
    	$this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	if($this->_request->isPost()){
    		$user = strip_tags($this->_request->getParam('username'));
    		$psw = strip_tags($this->_request->getParam('password'));
    		if(!empty($user) && !empty($psw)){
    			$mUser = new Administrator_Model_Users();
    			$mUser->username = $user;
    			$mUser->password = $psw;
    			$flag = false;
    			if($mUser->login()){
    				$flag = true;
    			}
    			if($flag == true){
    				$json['success'] = true;
    				echo Zend_Json::encode($json);
    			}else{
    				$json['success'] = false;
    				echo Zend_Json::encode($json);
    			}
    		}
    	}
    }
    public function outAction(){
    	$this->_helper->viewRenderer->setNoRender(true);
    	$auth = Zend_Auth_Admin::getInstance();
    	$auth->clearIdentity();
    	$this->_redirect('administrator/login');
    }


}

